﻿/**
* ...
* @author DefaultUser (Tools -> Custom Arguments...)
*/
package com.xuanque.mahjongDice.core {
	
	import com.xuanque.mahjongDice.util.ArrayUtilities;
	
	public class GameTip{
		
		private var pai_array:Array;
		private var pai_length:uint;
		private var range_array:Array;//归类
		
		private var jiang:uint;
		
		private static var Instance:GameTip = new GameTip();
		
		public function GameTip() {
			
		}
		
		/*
		 * 单件类的实例 
		*/
		public static function getInstance():GameTip {
			
			return Instance;
		}
		
		/*
		 * 注销 唯一的单件   相当于  public static var Instance:GameTip = new GameTip();
		*/
		public static function writeOffInstance():void {
			
			Instance = new GameTip();
		}
		
		public function setStaticObjectNull():void{
		    Instance = null;
		}
		
		
		/*
		 * 接收外部传递的游戏数组Array
		 * 
		 * */
		public function setJoinPaiArray(paiArray:Array):Boolean {
			this.pai_array = paiArray;
			this.pai_length = paiArray.length;
			
			return rangePaiArray(pai_array);
		}
		
		private function rangePaiArray(paiArray:Array):Boolean {
			
			this.range_array = this.isEqual(paiArray);
			
			//下面特殊情况时：range_array = [2,0,2,0,2,0,0,2];  暗七对两番
			var two_array:Array = ArrayUtilities.findMatchIndices(range_array, 2);//暗七对两番
			var four_array:Array = ArrayUtilities.findMatchIndices(range_array, 4);
			
			var two_length:uint = two_array.length;
			var four_length:uint = four_array.length;
			if ( two_length == 4) { //比较相同数的个数
				
				return true
			}else if ( four_length == 1 && two_length == 2) {
				
				return true
			}
			
			return this.getNormalWin(range_array);
		}
		
		
		
		
		
		//存在的类型： 11 222 333 , 11 222 123 , 11 123 456
		private function getNormalWin(paiArray:Array):Boolean {
			
			var _resultString:Boolean=true;
				for (var i:int=0; i < paiArray.length; i++) {
						if (paiArray[i] != 0) {
							if (paiArray[i] >= 3) {//算是刻子
								paiArray[i] -=3;
								//trace(":::::::::MahJongDice 类:::::::::  刻子+++++++++++++++++++++++++++++++++++");
								
								if (getNormalWin(paiArray)) {
									return true
								}else {
									paiArray[i] +=3;
								}
							} 
							
							if (!jiang && paiArray[i] >= 2) {
								
								jiang = 1;
								paiArray[i] -=2 ;
								
								//trace(":::::::::MahJongDice 类:::::::::  将+++++++++++++++++++++++++++++++++++");
								
								if (getNormalWin(paiArray)) {
									return true
								}else {
									
									paiArray[i] += 2 ;
									jiang = 0;
								}
								
							} 
							
							
							
							if (paiArray[i] >= 1 && paiArray[i + 1] >= 1 && paiArray[i + 2] >= 1) {//顺子
								//下面情况时：range_array = [0,0,0,2,2,2,2,0];
								paiArray[i] -= 1;
								paiArray[i + 1] -= 1;
								paiArray[i + 2] -= 1;
								
								//trace(":::::::::MahJongDice 类:::::::::  顺子+++++++++++++++++++++++++++++++++++");
								
								if (getNormalWin(paiArray)) {
									return true
								}else {
									paiArray[i] += 1;
									paiArray[i + 1] += 1;
									paiArray[i + 2] += 1;
								}
							}else {
								
								_resultString = false; //这里不能返回，
								//"不存在 [万 条 饼 风] 中的一个系列"
								
								
							}
						}	
			}
			
			
			if (jiang < 1) {
				//trace(":::::::::MahJongDice 类:::::::::  必需要有一个将 jiang");
				_resultString = false;
			}
			
			
			return _resultString;
			
		}
		
		
		/*
		 * 
		 *判断可能存在所有相同的情况 ,只能针对于一维数组，不是二维
		 * 
		 * 作用:提取出一个数组,转换成一个类型数组,比如:[1,2,3,4,4,6,6,6]  --->  [1,1,1,2,0,3]
		 * 
		 * var old_array = [1,2,3,4,4,6,6,6]
		 * 
		 * var new_array = this.isEqual(old_array);
		*/
		
		private function isEqual(InArray:Array):Array{
			var result_array:Array;
			var return_array:Array = new Array();
			for (var i = 1; i <= 6; i++) {//只有从一点到六点
				result_array = ArrayUtilities.findMatchIndices(InArray, i);
				//trace("比较相同数的个数 ：" + result_array.length);
				return_array.push(result_array.length);
			}
			return return_array;
		}
		
		
		
		
	}	
}